import { filter, map, mergeMap } from 'rxjs/operators';

import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { Title } from '@angular/platform-browser';

import { Consts } from './configs/consts';
import { Menus } from './configs/menus';
import { ChatService } from './services/chat.service';
import { UserInfo } from './classes/user-info';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent implements OnInit {

  title = Consts.Title;
  menus = Menus;

  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private titleService: Title,
    private chatService: ChatService
  ) { }

  ngOnInit() {
    //路由事件:设置标题
    this.router.events
      .pipe(filter(event => event instanceof NavigationEnd))
      .pipe(map(() => this.activatedRoute))
      .pipe(map(route => {
        while (route.firstChild) route = route.firstChild;
        return route;
      }))
      .pipe(filter(route => route.outlet === 'primary'))
      .pipe(mergeMap(route => route.data))
      .subscribe((event) => this.titleService.setTitle(event['title']));
    
      this.chatService.login({id: 0, nickname:'用户0'});
  }
}
